<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="学员编号" prop="stuCode">
        <el-input v-model="queryParams.stuCode" placeholder="请输入学员编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学员姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入学员姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程名称" prop="className">
        <el-input v-model="queryParams.className" placeholder="请输入课程名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程分类名称" prop="classTypeName">
        <el-input v-model="queryParams.classTypeName" placeholder="请输入课程分类名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="门店编号" prop="storeCode">
        <el-input v-model="queryParams.storeCode" placeholder="请输入门店编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="门店名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入门店名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学管姓名" prop="leaName">
        <el-input v-model="queryParams.leaName" placeholder="请输入学管姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="教练姓名" prop="coaName">
        <el-input v-model="queryParams.coaName" placeholder="请输入教练姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上课状态" prop="classStatus">
        <el-select v-model="queryParams.classStatus" placeholder="请选择上课状态" clearable @change="handleQuery">
          <el-option label="已上课" value="0" />
          <el-option label="未上课" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!this.queryParams.stuId">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['course:planClass:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planClassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!this.queryParams.stuId" />
      <el-table-column label="学员编号" align="center" prop="stuCode" width="120px" />
      <el-table-column label="学员姓名" align="center" prop="stuName" />
      <el-table-column label="课程名称" align="center" prop="className" />
      <el-table-column label="课程分类名称" align="center" prop="classTypeName" width="100px" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="!this.queryParams.stuId"
        width="300px">
        <template slot-scope="scope">
          <el-button type="warning"  v-if="scope.row.orderType =='1'" @click="handleUpdate(scope.row, 0)">扣绩效</el-button>
          <el-button type="success" @click="handleUpdate(scope.row, 1)">数据查看</el-button>
          <el-button v-if="scope.row.classStatus != '0' && scope.row.coachOrderStatus != '1'" type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="门店编号" align="center" prop="storeCode" width="120px" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="学管姓名" align="center" prop="leaName" />
      <el-table-column label="教练姓名" align="center" prop="coaName" />
      <el-table-column label="上课时间" align="center" prop="upClassTime" />
      <el-table-column label="共计学时" align="center" prop="learnNum" />
      <el-table-column label="上课状态" align="center" prop="classStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.classStatus === '0'" type="success">已上课</el-tag>
          <el-tag v-else-if="scope.row.classStatus === '1'" type="danger">未上课</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime" />
      <!-- <el-table-column label="创建人" align="center" prop="createBy" /> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改排课信息对话框 -->

    <el-dialog title="扣绩效" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="扣款金额" prop="changeAmount">
          <el-input-number v-model="form.changeAmount" placeholder="请输入扣款金额" />
        </el-form-item>
        <el-form-item label="扣款原因" prop="changeDetails">
          <el-input type="textarea" v-model="form.changeDetails" placeholder="请输入扣款原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormKhx">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 数据查看 -->
    <el-dialog title="数据查看" :visible.sync="openView" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="日期" prop="stuName" label-width="100px">
          <el-input v-model="form.stuName" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="stuName" label-width="100px">
          <el-input v-model="form.stuName" disabled />
        </el-form-item>
        <!-- <el-form-item label="年级" prop="changeAmount">
          <el-input v-model="form.changeAmount" disabled />
        </el-form-item> -->
        <el-form-item label="学员编号" prop="stuCode" label-width="100px">
          <el-input v-model="form.stuCode" disabled />
        </el-form-item>
        <el-form-item label="教练" prop="coaName" label-width="100px">
          <el-input v-model="form.coaName" disabled />
        </el-form-item>
        <el-form-item label="学习状况反馈" prop="learnRemark" label-width="100px">
          <el-input v-model="form.learnRemark" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="cancelView">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPlanClass,
  getPlanClass,
  delPlanClass,
  addPlanClass,
  updatePlanClass,
  updatePlanClassKjx
} from '@/api/course/planClass'
import store from '@/store'

export default {
  name: "PlanClass",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排课信息表格数据
      planClassList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuCode: null,
        stuName: null,
        className: null,
        classTypeName: null,
        storeCode: null,
        storeName: null,
        leaName: null,
        coaName: null,
        classTime: null,
        starTime: null,
        endTime: null,
        learnNum: null,
        teachMethod: null,
        classStatus: null,
        createBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排课信息列表 */
    getList() {
      this.loading = true;
      const stuId = this.$route.query.stuId;
      if (stuId) {
        this.queryParams.stuId = stuId; // 如果有学员ID，则设置查询参数
      } else {
        this.queryParams.stuId = null; // 否则清除学员ID
      }
      //是否试课：0试课  1正式课
      const orderType = this.$route.query.orderType;
      // console.log('[ isTest ] >', isTest)
      this.queryParams.orderType = orderType;

      listPlanClass(this.queryParams).then(response => {
        this.planClassList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 修复页面刷新后参数丢失的问题
        this.$router.replace({
          path: this.$route.path,
          query: {
            ...this.$route.query
          }
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelView() {
      this.openView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        stuId: null,
        stuCode: null,
        stuName: null,
        className: null,
        classTypeId: null,
        classTypeName: null,
        storeUserId: null,
        storeCode: null,
        storeName: null,
        orderType: null,
        leaId: null,
        leaName: null,
        coaId: null,
        coaName: null,
        salary: null,
        perSalary: null,
        classTime: null,
        starTime: null,
        endTime: null,
        learnNum: null,
        teachMethod: null,
        classStatus: null,
        review: null,
        learnRemark: null,
        coachRemark: null,
        coachOrderStatus: null,
        offFormal: null,
        offTest: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row, val) {
      this.reset();
      const id = row.id || this.ids
      getPlanClass(id).then(response => {
        this.form = response.data;
        if (val == 0) {
          //扣绩效
          this.open = true;
          this.title = "扣绩效";
        } else {
          //数据查看
          this.openView = true
          this.title = "数据查看";
        }
      });
    },
    submitFormKhx(){
      updatePlanClassKjx(this.form).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.form.countType = '1'; // 设置扣款类型为1 减
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlanClass(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlanClass(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排课信息编号为"' + ids + '"的数据项？').then(function () {
        return delPlanClass(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/planClass/export', {
        ...this.queryParams
      }, `planClass_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
